//
//  WYTTimeTool.m
//  Diary
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "WYTTimeTool.h"

@implementation WYTTimeTool

+ (NSString *)currentSystemDateString{
    NSDate *currentTime = [NSDate date];
    
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    NSRange monthRange = NSMakeRange(4, 2);
    NSRange dateRange = NSMakeRange(6, 2);
    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    
    return [NSString stringWithFormat:@"%@%@%@%@%@", yearString, monthString, dateString, hourString, minutesString];
}

+ (NSString *)currentDate_yearMonthDateHourMinutesSeparatByChinese{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    NSRange monthRange = NSMakeRange(4, 2);
    NSRange dateRange = NSMakeRange(6, 2);
    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    
    return [NSString stringWithFormat:@"%@年%@月%@日 %@:%@", yearString, monthString, dateString, hourString, minutesString];
}

+ (NSString *)currentDate_yearMonthDateHourMinutesSeparatByDiagonals{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    NSRange monthRange = NSMakeRange(4, 2);
    NSRange dateRange = NSMakeRange(6, 2);
    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    NSString *currentamOrPm = nil;
    if (hourString.integerValue >= 12) {
        currentamOrPm = @"pm";
    } else {
        currentamOrPm = @"am";
    }
    
    return [NSString stringWithFormat:@"%@/%@/%@ %@:%@%@", yearString, monthString, dateString, hourString, minutesString,currentamOrPm];
}

+ (NSString *)currentDate_yearMonthDateHourMinutesSeparatByDots{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    NSRange monthRange = NSMakeRange(4, 2);
    NSRange dateRange = NSMakeRange(6, 2);
    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    
    return [NSString stringWithFormat:@"%@.%@.%@ %@:%@", yearString, monthString, dateString, hourString, minutesString];
}

+ (NSString *)currentYear{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange yearRange = NSMakeRange(0, 4);
    
    NSString *yearString = [currentTimeString substringWithRange:yearRange];
    
    
    return yearString;
}

+ (NSString *)currentMonth{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange monthRange = NSMakeRange(4, 2);
    
    NSString *monthString = [currentTimeString substringWithRange:monthRange];
    
    return monthString;
}

+ (NSString *)currentDate{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
    
    NSRange dateRange = NSMakeRange(6, 2);
    
    NSString *dateString = [currentTimeString substringWithRange:dateRange];
    
    return dateString;
}

+ (NSString *)currentDay{
    NSDate *nowDate = [NSDate date];
    NSString *dayString = [[nowDate descriptionWithLocale:[NSLocale currentLocale]] substringWithRange:NSMakeRange(14, 1)];
    if ([dayString isEqualToString:@"一"]) {
        return @"Mon";
    }else if([dayString isEqualToString:@"二"]){
        return @"Tue";
    }else if([dayString isEqualToString:@"三"]){
        return @"Wed";
    }else if([dayString isEqualToString:@"四"]){
        return @"Thur";
    }else if([dayString isEqualToString:@"五"]){
        return @"Fri";
    }else if([dayString isEqualToString:@"六"]){
        return @"Sat";
    }else{
        return @"Sun";
    }
}

+ (NSString *)currentHourMinutes{
    
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];

    NSRange hourRange = NSMakeRange(8, 2);
    NSRange minuteRange = NSMakeRange(10, 2);

    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    NSString *minutesString = [currentTimeString substringWithRange:minuteRange];
    
    return [NSString stringWithFormat:@"%@:%@", hourString, minutesString];
}

+ (NSString *)currentamOrPm{
    NSDate *currentTime = [NSDate date];
    [currentTime descriptionWithLocale:NSLocaleCountryCode];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];

    NSRange hourRange = NSMakeRange(8, 2);
    
    NSString *hourString = [currentTimeString substringWithRange:hourRange];
    
    if (hourString.integerValue > 12) {
        return @"pm";
    }else{
        return @"am";
    }
}

#pragma mark --- 计算时间差 (时间轴用得到)
+ (NSString *)NSTimerAboutTime:(NSString *)created
{
//    NSString *dateStr = created;// 2012-05-17 11:23:23
    NSString *yearStr = [[created substringFromIndex:0] substringToIndex:4];
    NSString *monthStr = [[created substringFromIndex:5] substringToIndex:2];
    NSString *dayStr = [[created substringFromIndex:8] substringToIndex:2];
    NSString *hourStr = [[created substringFromIndex:11] substringToIndex:2];
    NSString *minteStr = [[created substringFromIndex:14] substringToIndex:2];
    NSString *secondStr = @"00";
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@:%@", yearStr, monthStr, dayStr, hourStr, minteStr, secondStr];

    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromdate=[format dateFromString:dateStr];
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:fromDate toDate:localeDate options:0];
    NSInteger months = [components month];
    NSInteger days = [components day];
    
    if (months==0&&days==0) {
        dateStr = @"今天";
        return dateStr;
    }else if(months==0&&days==1){
        dateStr= @"昨天";
        return dateStr;
    }else{
        dateStr=[[dateStr substringFromIndex:5]substringToIndex:5];
        //NSLog(@"NSTimerAboutTime -- %@", dateStr);
        return [NSString stringWithFormat:@"%@", dateStr];
    }
    return nil;
}


@end
